<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <FormPanel ref="formPanel" :list="formList" :cols="1" :formType="type" labelWidth="100" :isSubmitBtn="false" style="margin-left: 20px;margin-right: 50px;"/>
      <div style="padding-top: 1rem;"></div>
      <div :style="{
          position: 'absolute',
          left: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right'
        }">
        <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
    </Spin>
  </div>
</template>
<script>
import { saveOne,updateById } from '@/api/claim/factory/AdditionalWarrantyItems';
import { dictionary } from '@/utils/dictMixin';
import { notifyAction } from '@/utils';
import { includeZeroDouble,labourCode8 } from '@/utils/validate';
export default {
  mixins: [dictionary],
  props: {
    formData: {
      type: Object,
      default: () => ({})
    },
    type: {
      type: String
    },
  },
  components: {},
  data() {
    return {
      fetchParams: {}, //ipa 接口
      formList: this.createFormList(),
      loading: false,
    };
  },
  created() {
    this.chuqi();
  },
  methods: {
    async chuqi() {
      if(this.type === "edit"){
        this.formList.forEach(x => {
          x.initialValue = this.formData[x.fieldName];
          if(x.fieldName === 'labourCode'){
            x.disabled = true;
          }
        });
      }
    },
    async saveDialog() {
      let res = null;
      const [err, data] = await this.$refs.formPanel.GET_FORM_DATA();
      if (err) return;
        this.loading = true;
      if(this.type === "add"){
        res = await saveOne({...data});
      }
      if(this.type === "edit"){
        data.itemId = this.formData.itemId;
        res = await updateById({...data});
      }
      if (res.resultCode === 200) {
        if (res.errMsg) {
          notifyAction(res.errMsg, 'error');
        } else {
          notifyAction('保存成功！');
          this.closeDialog();
        }
      }else{
        this.loading = false;
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', false);
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '附加项目代码',
          fieldName: 'labourCode',
          initialValue: '',
          placeholder: '请输入附加项目代码...',
          rules: [
            { required: true,trigger: 'blur',  message: "请输入附加项目代码...", },
            { required: true,trigger: 'blur' ,validator: labourCode8 }
          ]
        },
        {
          type: 'INPUT',
          label: '附加项目名称',
          fieldName: 'labourName',
          initialValue: '',
          placeholder: '请输入附加项目名称...',
          rules: [{ required: true, message: "请输入附加项目名称...", trigger: 'blur' }],
          maxlength: 100
        },
        {
          type: 'INPUT',
          label: '附加项目费用',
          fieldName: 'labourPrice',
          initialValue: 0.00,
          placeholder: '请输入附加项目费用...',
          rules: [
            { required: true,  message: "请输入附加项目费用...", trigger: 'blur'},
            { required: true, validator: includeZeroDouble , trigger: 'blur'}
          ],
          maxlength: 9
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          initialValue: 10041001,
          itemList: this.createDictList(1004)
        }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
